import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {ContractLabel} from "../../../core/services/types";
import {Observable, of} from "rxjs";
import {fnCheckForm} from "../../../utils/tools";

@Component({
  selector: 'app-label-type-modal',
  templateUrl: './label-type-modal.component.html',
  styleUrls: ['./label-type-modal.component.less']
})
export class LabelTypeModalComponent implements OnInit {
  addEditForm!: FormGroup;
  params!: ContractLabel;
  isEdit = false;

  constructor(private fb: FormBuilder,) {
  }

  initForm(): void {
    this.addEditForm = this.fb.group({
      typeName: [null, [Validators.required]],
    });
  }

  async ngOnInit(): Promise<void> {
    this.initForm();
    if (Object.keys(this.params).length > 0) {
      this.isEdit = true;
      //  console.log(this.params);
      this.addEditForm.patchValue(this.params);
    }
  }

  // 此方法为如果有异步数据需要加载，则在该方法中添加
  protected getAsyncFnData(modalValue: any): Observable<any> {
    return of(modalValue);
  }

  // 返回false则不关闭对话框
  protected getCurrentValue(): Observable<any> {
    if (!fnCheckForm(this.addEditForm)) {
      return of(false);
    }
    return of(this.addEditForm.value);
  }

}
